package com.cloudfun.campusshare.service.impl;

import cn.hutool.core.lang.Snowflake;
import com.alibaba.fastjson.JSON;
import com.cloudfun.campusshare.common.constant.AttachType;
import com.cloudfun.campusshare.common.constant.Code;
import com.cloudfun.campusshare.common.constant.MerchantRegisterStatus;
import com.cloudfun.campusshare.common.constant.RegisterType;
import com.cloudfun.campusshare.common.exception.BusinessException;
import com.cloudfun.campusshare.common.model.dto.ImageDataBatchUploadDTO;
import com.cloudfun.campusshare.common.model.dto.ImageDataBatchUploadRespDTO;
import com.cloudfun.campusshare.common.model.vo.BackCardBindVO;
import com.cloudfun.campusshare.common.security.AuthenticationHolder;
import com.cloudfun.campusshare.entity.AttachmentEntity;
import com.cloudfun.campusshare.entity.MerchantBankRegisterEntity;
import com.cloudfun.campusshare.repo.AttachmentEntityRepo;
import com.cloudfun.campusshare.repo.MerchantBackRegisterEntityRepo;
import com.cloudfun.campusshare.service.AttachmentService;
import com.cloudfun.campusshare.service.HsqService;
import com.cloudfun.campusshare.service.MerchantRegisterService;
import com.cloudfun.campusshare.util.AliyunOSSUtil;
import com.cloudfun.campusshare.util.BeanCopyUtil;
import com.cloudfun.campusshare.util.DateUtil;
import com.cloudfun.campusshare.util.OrderNoGenUtil;
import com.cloudfun.campusshare.util.hsq.StringUtil;
import lombok.SneakyThrows;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Base64Utils;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Objects;

/**
 * @author : zhangyongjie
 * @version : 1.0
 * @createTime : 2021/3/25 20:15
 * @description : 商户进件
 */
@Service
public class MerchantRegisterServiceImpl implements MerchantRegisterService {
    @Autowired
    private MerchantBackRegisterEntityRepo merchantBackRegisterEntityRepo;
    @Autowired
    private AttachmentService attachmentService;
    @Resource
    private AttachmentEntityRepo attachmentEntityRepo;
    @Autowired
    private HsqService hsqService;
    @Autowired
    private AliyunOSSUtil aliyunOSSUtil;
    @Autowired
    private OrderNoGenUtil orderNoGenUtil;
    @Override
    @SneakyThrows
    public void submitRegisterRequest(BackCardBindVO backCardBindVO) {
        if(RegisterType.ORGANIZATION.equals(backCardBindVO.getRegisterType()) &&
                (StringUtils.isBlank(backCardBindVO.getEmail()) || StringUtils.isBlank(backCardBindVO.getBusinessLicense())
                || StringUtils.isBlank(backCardBindVO.getBusinessLicenseImg()))){
            throw new BusinessException(Code.PARAMETER_ERROR);
        }

        MerchantBankRegisterEntity merchantBankRegisterEntity = BeanCopyUtil.copy(backCardBindVO,
                MerchantBankRegisterEntity.class, "registerType");
        merchantBankRegisterEntity.setTransNo(orderNoGenUtil.next0("","MERCHANT_REGISTER"));
        merchantBankRegisterEntity.setUserId(AuthenticationHolder.getUserId());
        merchantBankRegisterEntity.setStatus(MerchantRegisterStatus.NOT_BEGIN);
        MerchantBankRegisterEntity save = merchantBackRegisterEntityRepo.save(merchantBankRegisterEntity);

        //上传资料
        ImageDataBatchUploadDTO imageDataBatchUploadDTO = new ImageDataBatchUploadDTO();
        imageDataBatchUploadDTO.setRequestDate(DateUtil.localDateToString(LocalDateTime.now()));
        //TODO:
        imageDataBatchUploadDTO.setReturnUrl("");
        ImageDataBatchUploadDTO.Qualification qualification = new ImageDataBatchUploadDTO.Qualification();
        AttachmentEntity idCardFrontImg = attachmentEntityRepo.findById(backCardBindVO.getIdCardFrontImg()).get();
        AttachmentEntity idCardBackImg = attachmentEntityRepo.findById(backCardBindVO.getIdCardBackImg()).get();
        AttachmentEntity businessLicenseImg = null;
        if(RegisterType.ORGANIZATION.equals(backCardBindVO.getRegisterType())){
            businessLicenseImg = attachmentEntityRepo.findById(backCardBindVO.getBusinessLicenseImg()).get();
        }

        qualification.setIdCardFrontImg(Base64Utils.encodeToString(IOUtils.toByteArray(aliyunOSSUtil.getObject(idCardFrontImg.getObjName()))));
        qualification.setIdCardBackImg(Base64Utils.encodeToString(IOUtils.toByteArray(aliyunOSSUtil.getObject(idCardBackImg.getObjName()))));
        if(Objects.nonNull(businessLicenseImg)){
            qualification.setBusinessLicenseImg(Base64Utils.encodeToString(IOUtils.toByteArray(aliyunOSSUtil.getObject(businessLicenseImg.getObjName()))));
        }
        imageDataBatchUploadDTO.setQualification(JSON.toJSONString(qualification));
        ImageDataBatchUploadRespDTO imageDataBatchUploadRespDTO = hsqService.uploadImageData(imageDataBatchUploadDTO);
        save.setBatchNo(imageDataBatchUploadRespDTO.getBatchNo());
        merchantBackRegisterEntityRepo.save(merchantBankRegisterEntity);

        //关联文件
        attachmentService.relateAttachment(save.getId(),idCardFrontImg.getId(), AttachType.ID_CARD_FRONT);
        attachmentService.relateAttachment(save.getId(),idCardBackImg.getId(), AttachType.ID_CARD_BACK);
        if(RegisterType.ORGANIZATION.equals(backCardBindVO.getRegisterType())){
            attachmentService.relateAttachment(save.getId(),businessLicenseImg.getId(), AttachType.LICENSE);
        }
    }
}
